package com.example.livedatatest;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class ViewModelWithLiveData extends ViewModel {
    private MutableLiveData<Integer> LikedNumber;

    //构造函数
    //在get里面判断了 所以这里可以不用写构造函数了
//    ViewModelWithLiveData(){
//        LikedNumber = new MutableLiveData<>();
//        LikedNumber.setValue(0);
//    }

    public MutableLiveData<Integer> getLikedNumber() {
        if(LikedNumber == null){
            LikedNumber = new MutableLiveData<>();
            LikedNumber.setValue(0);
        }
        return LikedNumber;
    }

    public void addLikedNumber(int n){
        if (LikedNumber.getValue() == 0 && n == -1){
            return;
        }
        LikedNumber.setValue(LikedNumber.getValue() + n);
    }
}
